﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using O.M.Production.Application.IService;
using O.M.Production.Application.Service;
using O.M.Production.Domain.DTO.Produce_work_order;
using O.M.Production.Domain.DTO.quality_inspection_management;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using System.Linq.Expressions;

namespace O.M.Production.Read.API.Controllers
{
    /// <summary>
    /// Api控制器-质检管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Read_Quality_Inspection_ManagementController : ControllerBase
    {
        private readonly I_quality_inspection_management_Service _quality_Inspection_Management_Repository;

        public Read_Quality_Inspection_ManagementController(I_quality_inspection_management_Service quality_Inspection_Management_Repository)
        {
            _quality_Inspection_Management_Repository = quality_Inspection_Management_Repository;
        }

        /// <summary>
        /// 分页查询-质检管理
        /// </summary>
        /// <param name="QIMId_Name">编号或名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_quality_inspection_management(string? QIMId_Name, int pageSize, int pageIndex)
        {
            try
            {
                var list = await _quality_Inspection_Management_Repository.Get_quality_inspection_management(QIMId_Name, pageSize, pageIndex);
                return Ok(list);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 导出列表到Excel-质检管理
        /// </summary>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> Post_Derive_Excel()
        {
            try
            {
                var list = await _quality_Inspection_Management_Repository.GetShowInforAsync();
                var ms = new MemoryStream();
                ms.SaveAs(list);
                ms.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "质检管理.xlsx"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 返回单个实体-质检管理
        /// </summary>
        /// <param name="id">质检编号</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_quality_inspection_management_Model(string id)
        {
            try
            {
                var res = await _quality_Inspection_Management_Repository.Get_quality_inspection_management_Model(id);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改-质检管理
        /// </summary>
        /// <param name="quality">质检管理实体</param>
        /// <returns>统一返回格式</returns>
        [HttpPut]
        public async Task<IActionResult> Put_Upd_quality_inspection_management(quality_inspection_management quality)
        {
            try
            {
                var res = await _quality_Inspection_Management_Repository.Put_Upd_quality_inspection_management(quality);
                return Ok(res);
            }
            catch (Exception)
            {
                _quality_Inspection_Management_Repository.RollbackTranInfor();
                throw;
            }
        }
        /// <summary>
        /// 质检-生产工单
        /// </summary>
        /// <param name="queality_Testing_DTO">质检DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPut]
        public async Task<IActionResult> Post__quality_testing(Queality_Testing_DTO queality_Testing_DTO)
        {
            try
            {
                var res = await _quality_Inspection_Management_Repository.Post__quality_testing(queality_Testing_DTO);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
